package player

import (
	"fmt"

	. "7qule.com/sd3/CrossServer/src/bll/player/bll"
	"7qule.com/sd3/CrossServer/src/common/dataInterchange"
	"7qule.com/sd3/CrossServer/src/webServer"
	"7qule.com/sd3/rpcService/Common"
	"public.com/Framework/managecenterMgr"
	"public.com/goutil/logUtil"
)

func init() {
	webServer.RegisterFunction(new(PlayerApi))
}

// 玩家请求接口模块
type PlayerApi struct {
}

func (this *PlayerApi) UpdatePlayer(id string, partnerId int32, serverId int32, serverName string, name string, guildId string, guildName string, lv int32, vip int32, fap, maxFap int64, headImageId int32, fashionModelId int32, fashionLevel int32, flag string, logo string, baseInfo, clientPlayerStr string) *webServer.ResponseObject {
	//获取相应信息
	responseObj := webServer.GetInitResponseObj()

	_, serverObj, exists := managecenterMgr.GetServerGroup(partnerId, serverId)
	if exists == true {
		serverName = serverObj.Name
	}

	// 更新玩家信息数据
	GlobalPlayerBll.UpdateInfo(id, serverObj.GroupId, partnerId, serverId, serverName, name, guildId, guildName, lv, vip, fap, maxFap, headImageId, fashionModelId, fashionLevel, flag, logo, baseInfo, clientPlayerStr)

	//返回成功状态
	return responseObj.SetResultStatus(Common.ResultStatus_Success)
}

// 获取玩家信息
func (this *PlayerApi) GetPlayerInfo(partnerId, serverId int32, playerId string) *webServer.ResponseObject {
	//获取相应信息
	responseObj := webServer.GetInitResponseObj()

	sgObj, _, exists := managecenterMgr.GetServerGroup(partnerId, serverId)
	if exists == false {
		return responseObj.SetResultStatus(Common.ResultStatus_Cross_ServerGroupNoExists)
	}

	// 去对应地址获取玩家信息
	url := fmt.Sprintf(sgObj.GetGSCallbackUrl("GetPlayerInfoApi?PlayerId=%s"), playerId)
	infoData, err := dataInterchange.GetWebData(url)
	if err != nil {
		logUtil.ErrorLog("GetPlayerInfo, 获取玩家出错, url:%s  err:", url, err)
		return responseObj.SetResultStatus(Common.ResultStatus_DataError)
	}

	//res, _ := json.Marshal(infoData)

	//返回成功状态
	responseObj.SetResultStatus(Common.ResultStatus_Success)
	responseObj.Data = string(infoData)

	return responseObj
}

func (this *PlayerApi) GetHeroInfo(partnerId, serverId int32, playerId string, heroModelId int32) *webServer.ResponseObject {
	//获取相应信息
	responseObj := webServer.GetInitResponseObj()

	sgObj, _, exists := managecenterMgr.GetServerGroup(partnerId, serverId)
	if exists == false {
		return responseObj.SetResultStatus(Common.ResultStatus_Cross_ServerGroupNoExists)
	}

	// 去对应地址获取玩家信息
	url := fmt.Sprintf(sgObj.GetGSCallbackUrl("GetHeroInfoApi?PlayerId=%s&HeroModelId=%d"), playerId, heroModelId)
	infoData, err := dataInterchange.GetWebData(url)
	if err != nil {
		logUtil.ErrorLog("GetHeroInfo, 获取玩家出错, url:%s  err:", url, err)
		return nil
	}

	//res, _ := json.Marshal(infoData)

	//返回成功状态
	responseObj.SetResultStatus(Common.ResultStatus_Success)
	responseObj.Data = string(infoData)

	return responseObj
}

func (this *PlayerApi) UpdateGuild(guildId, guildName string, lv int32, flag, logo, bannerInfo string, serverGroupId, serverId int32, serverName string) *webServer.ResponseObject {
	//获取相应信息
	responseObj := webServer.GetInitResponseObj()

	// 更新公会信息数据
	GlobalGuildBll.UpdateInfo(guildId, guildName, lv, flag, logo, bannerInfo, serverGroupId, serverId,
		serverName)

	//返回成功状态
	return responseObj.SetResultStatus(Common.ResultStatus_Success)
}

func (this *PlayerApi) UpdateServerInfo(serverId int32, worldLv int32) *webServer.ResponseObject {
	//获取相应信息
	responseObj := webServer.GetInitResponseObj()

	//返回成功状态
	return responseObj.SetResultStatus(Common.ResultStatus_Success)
}

func (this *PlayerApi) GetPlayerStrategyInfo(partnerId, serverId int32, playerId string, strategyId int32) *webServer.ResponseObject {
	// 获取相应信息
	responseObj := webServer.GetInitResponseObj()

	sgObj, _, exists := managecenterMgr.GetServerGroup(partnerId, serverId)
	if exists == false {
		return responseObj.SetResultStatus(Common.ResultStatus_Cross_ServerGroupNoExists)
	}

	// 去对应地址获取玩家信息
	url := fmt.Sprintf(sgObj.GetGSCallbackUrl("GetPlayerStrategyInfoApi?PlayerId=%s&StrategyId=%v"), playerId, strategyId)
	infoData, err := dataInterchange.GetWebData(url)
	if err != nil {
		logUtil.ErrorLog("GetPlayerStrategyInfoApi, 获取玩家阵容出错, url:%s  err:", url, err)
		return responseObj.SetResultStatus(Common.ResultStatus_DataError)
	}

	// 返回成功状态
	responseObj.SetResultStatus(Common.ResultStatus_Success)
	responseObj.Data = string(infoData)

	return responseObj
}
